package com.pkk.cloud.support.admin.controller.interior.server;

import com.pkk.cloud.support.admin.dto.req.resources.OauthClientRoleResourcesReqDto;
import com.pkk.cloud.support.admin.dto.req.server.OauthServerAddReqDto;
import com.pkk.cloud.support.admin.dto.req.server.OauthServerDelReqDto;
import com.pkk.cloud.support.admin.dto.req.server.OauthServerIdReqDto;
import com.pkk.cloud.support.admin.dto.req.server.OauthServerPageSearchReqDto;
import com.pkk.cloud.support.admin.dto.req.server.OauthServerUpdateReqDto;
import com.pkk.cloud.support.admin.dto.resp.client.OauthClientServerSimpleRespDto;
import com.pkk.cloud.support.admin.dto.resp.server.OauthServerClientSimpleRespDto;
import com.pkk.cloud.support.admin.dto.resp.server.OauthServerListRespDto;
import com.pkk.cloud.support.admin.dto.resp.server.RoleClientResourceSimpeRespDto;
import com.pkk.cloud.support.admin.entity.resource.PenguinSysServer;
import com.pkk.cloud.support.admin.service.PenguinSysServerService;
import com.pkk.cloud.support.oauth2.client.aspect.annotation.PreAuth;
import com.pkk.cloud.support.oauth2.common.utils.SecurityUtils;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.request.PagedQueryRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 授权的服务的控制器
 * @author: peikunkun
 * @create: 2020-04-01 20:55
 **/
@Slf4j
@RestController
@RequestMapping("/oauth/server")
public class OauthServerController {

  @Resource
  private PenguinSysServerService penguinSysServerService;


  /**
   * @Description: 添加服务端操作(客户端组)
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:57
   */
  @PreAuth
  @PostMapping("add")
  public CommonResponse<Boolean> add(@Validated @RequestBody CommonRequest<OauthServerAddReqDto> request) {
    final PenguinSysServer server = new PenguinSysServer();
    BeanUtils.copyProperties(request.getBody(), server);
    server.setCreateTime(new Date());
    server.setCreateName(SecurityUtils.getUserId());
    return ResponseUtil.success(penguinSysServerService.saveSysServerService(server));
  }


  /**
   * @Description: 删除服务端操作(客户端组)
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("del")
  public CommonResponse<Boolean> del(@Validated @RequestBody CommonRequest<OauthServerDelReqDto> request) {
    return ResponseUtil.success(penguinSysServerService.delSysServerServiceById(request.getBody().getId()));
  }

  /**
   * @Description: 更新服务端操作(客户端组)列表
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("update")
  public CommonResponse<Boolean> update(@Validated @RequestBody CommonRequest<OauthServerUpdateReqDto> request) {
    final PenguinSysServer server = new PenguinSysServer();
    BeanUtils.copyProperties(request.getBody(), server);
    server.setUpdateTime(new Date());
    server.setUpdateName(SecurityUtils.getUserId());
    return ResponseUtil.success(penguinSysServerService.updateSysServerServiceById(server));
  }


  /**
   * @Description: 获取服务端列表操作(客户端组)
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("all")
  public CommonResponse<List<OauthServerListRespDto>> all() {
    return ResponseUtil
        .success(penguinSysServerService.findSysServerServiceList(OauthServerPageSearchReqDto.builder().build()));
  }


  /**
   * @Description: 分页获取服务端列表信息
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.util.List < com.pkk.cloud.support.admin.dto.resp.server.OauthServerListRespDto>>
   * @Author: peikunkun
   * @Date: 2020/4/4 0004 下午 9:02
   */
  @PreAuth
  @PostMapping("list")
  public CommonResponse<List<OauthServerListRespDto>> list(
      @Validated @RequestBody PagedQueryRequest<OauthServerPageSearchReqDto> request) {
    return ResponseUtil.success(penguinSysServerService.findSysServerServiceList(request.getBody()));
  }


  /**
   * @Description: 依据服务组id查询服务(客户端基础)信息
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.util.List < java.lang.Object>>
   * @Author: peikunkun
   * @Date: 2020/4/5 0005 上午 12:21
   */
  @PreAuth
  @PostMapping("getServerByServerGroupId")
  public CommonResponse<List<OauthClientServerSimpleRespDto>> getServerByServerGroupId(
      @Validated @RequestBody CommonRequest<OauthServerIdReqDto> request) {
    return ResponseUtil.success(penguinSysServerService.getServerByServerGroupId(request.getBody()));
  }

  /**
   * @Description: 获取服务客户端的信息【服务端->客户端】
   * @return: com.pkk.components.rpc.response.CommonResponse<java.util.List < com.pkk.cloud.support.admin.dto.resp.server.OauthServerClientSimpleRespDto>>
   * @Author: peikunkun
   * @Date: 2020/4/9 下午7:12
   */
  @PreAuth
  @PostMapping("getServerClient")
  public CommonResponse<List<OauthServerClientSimpleRespDto>> getServerClient() {
    return ResponseUtil.success(penguinSysServerService.getServerClient());
  }


  /**
   * @Description: 获取角色客户端的绑定和未绑定的资源信息
   * @Author: peikunkun
   * @Date: 2020/4/9 下午7:12
   */
  @PreAuth
  @PostMapping("getRoleClientResource")
  public CommonResponse<List<RoleClientResourceSimpeRespDto>> getRoleClientResource(
      @Validated @RequestBody CommonRequest<OauthClientRoleResourcesReqDto> request) {
    return ResponseUtil.success(penguinSysServerService.getRoleClientResource(request.getBody()));
  }


}
